<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.5/themes/default/easyui.css"></link>
	<link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.5/themes/icon.css"></link>
	<link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.5/demo/demo.css"></link>
	<script language="JavaScript"  type="text/javascript" src="js/jquery-easyui-1.5/jquery.min.js"></script>
	<script language="JavaScript"  type="text/javascript" src="js/jquery-easyui-1.5/jquery.easyui.min.js"></script>
<style type="text/css">
	a:link,a:visited{
 		text-decoration:none;  /*超链接无下划线*/
	}
	a:hover{
 		text-decoration:none;  /*鼠标放上去有下划线*/
	}
</style>

	<script type="text/javascript">
	 /*<![CDATA[*/ 
	            
	 var sortMax;
	 var sortMin;
	 
	 $(document).ready(  
			  function(){
				  
				  $('#dlg').dialog('close');
				  $('#dlg1').dialog('close');
				  getMM();
				  
				 
				  
				  $("#typename").textbox({onClickButton:function(e){
					  submitForm();
				  }});
				  
				  $('#typename').textbox('textbox').keydown(function (e) {
				      if (e.keyCode == 13) {
				    	  submitForm();
				      }
				  });
				  
				  $('#pp1').tooltip({
						position: 'bottom',
						content: '<span style="color:#fff">单击修改菜类名</span>',
						onShow: function(){
							$(this).tooltip('tip').css({
								backgroundColor: '#666',
								borderColor: '#666'
							});
						}
					});
				  
			  });
	 
	
	function showAuthority(name,url)
	{
		self.parent.addPanel(name,url);
	}
	
	
	function showFoodTypeList()
	{
		
		$.get("foodTypeList", function(data){
			 $("#table").datagrid("loadData", data);
			 
			});
	}
	
	
	 
	 	function getMM()
	 	{
	 		$.ajax(
	 			{
	 	        	url: "get_Max",
	 	        	async: false,//改为同步方式
	 	        	type: "GET",
	 	        	success: function (data) {
	 	        		sortMax = data;
	 	        		getMin();
	 	 			}
	 	 	});
	 		
	 		
	 		
			  
	 	}
	 	
	 	function getMin()
	 	{
	 		$.ajax(
		 			{
		 	        	url: "get_Min",
		 	        	async: false,//改为同步方式
		 	        	type: "GET",
		 	        	success: function (data) {
		 	        		sortMin = data;
		 	        		showFoodTypeList();
		 	 			}
		 	 	});
	 		 
	 	}
	 
	    function delFoodType(val)
	    {
	    	 $.messager.defaults= { ok: "确定",cancel: "取消" };
	    	 
	    	$.messager.confirm("操作提示","您确定要执行操作吗？",
	    		function (data)
	    	 	{
	    	            if (data)
	    	 			{
	    	            	var url = "delete_foodtype?id="+val;
	    			    	$.get(url, function(data){
	    			    		
	    			    		getMM();
	    			    		
	    					});
	    
	    	            }
	    	            
	    	     });
	
	    }
	    
	   
	    function MaxSort(sort,id)
	    {
	    	    var url = "update_MaxSort?id="+id+"&sort="+sort;
	    	    $.get(url, function(data){
	    			   getMM();
	    		});	    	           
	    }
	    
	    function MinSort(sort,id)
	    {
	    	    var url = "update_MinSort?id="+id+"&sort="+sort;
	    	    $.get(url, function(data){
	    			   getMM();
	    		});	    	           
	    }
	   
	    
	    function formatSort(val,row){
	    	var text='';

	    	if(val!=sortMin)
	    	{
	    		text = '&nbsp;<a href="#"  onclick="MinSort(\''+val+'\',\''+row.id+'\')"><img width="20px" border="0" height="20px" src="img/up.png" title="向上"/></a>&nbsp;';
	    	}
	    	if(val !=sortMax)
	    	{
	    		text = text +'&nbsp;<a href="#"  onclick="MaxSort(\''+val+'\',\''+row.id+'\')"><img width="20px" border="0" height="20px" src="img/down.png" title="向下"/></a>&nbsp;';
	    	}
	    	
			return text;
		
		}
	    
	 	function formatColumn(val,row){
				return '<a href="#" onclick="loadAllandGroup(\''+val+'\',\''+row.typename+'\')">查看</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#"  onclick="delFoodType(\''+val+'\')">删除</a>';
			
		}
	 	
	 	function addfoodtype()
	 	{
	 		 $.messager.prompt("操作提示", "请输入菜品名", function (data) {
	             if (data) {
	                 alert(data);
	             }
	         });
	 	}
	 	
	 	function submitForm(){
	 		var name = $("#typename").val();
	 		if(name.length==0)
	 		{
	 			alert("菜类名称不能为空");
	 			return;
	 		}
	 			
			$.post("add_foodtype",
					$('#ff').serialize(),
					  function(data){
						getMM();
						$('#dlg').dialog('close');
						clearForm();
					  },
					  "text");
		}
		function clearForm(){
			$('#ff').form('clear');
		}
		
		function opendlg()
		{
			$('#dlg').dialog('open');
			$("#typename").textbox("setValue", "");
			$('#typename').textbox('textbox').focus(); 
		}
		
		function onClickRow(index,field){
		 	
	 		$('#table').datagrid('selectRow', index)
			.datagrid('beginEdit', index);
 		}
		
		
 	
 		function afterEdit(rowIndex,rowData,changes)
 		{
 			var typename = encodeURIComponent(rowData.typename);
 			var url = "update_foodtype?id="+rowData.id+"&typename="+typename;
 		
 			$.get(url, function(data){
 				 getMM();
			});
    	
 		}
 		
 		 function loadAllandGroup(foodtypeid,text)
 		   {
 			   var url ="foodListGroup?id="+foodtypeid;
 				  $.get(url, function(data){
 						$("#table1").datagrid("loadData", data.groupList);
 						
 					});
 				 $( "#dlg1" ).dialog({
 					  title: text,
 				      modal: true
 				    });
 				 $('#dlg1').dialog('open');
 		   }
 		
 		
 		 
 		function formatColumn1(val,row){
	 		var surl ="show_fooddetail?id="+val;
			return '<a href="#" onclick="self.parent.addPanel(\''+row.foodname+'\',\''+surl+'\')">查看</a>'
			
		}
 		
 
 	
 	function afterEdit1(rowIndex,rowData,changes)
 	{
 		var url = "udpatefood?id="+rowData.id+"&price="+rowData.price;
 		
 		$.get(url, function(data){
		});
    	
 	}
 	
 	function endedit1()
 	{
 		var row = $('#table1').datagrid('getSelected');
 		var index = $('#table1').datagrid('getRowIndex',row);
		$('#table1').datagrid('endEdit', index);
 	}
 	
 	function cellclick2(index, field){
        if(field=='typename')
        {
      		$('#table')
				.datagrid('beginEdit', index);
        }else
        {
       	 $('#table')
				.datagrid('endEdit', index);
        }
   }
 	
 	function cellclick3(index, field){
        if(field=='price')
        {
      		$('#table1')
				.datagrid('beginEdit', index);
        }else
        {
       	 $('#table1')
				.datagrid('endEdit', index);
        }
   }
 	
	 	
	 	 /*]]>*/  
	</script>
	
	 
</head>
<body>

<div  id="pp1" style="position:absolute;left:0px;top:0px;width:510px;height:90%;">
<table id="table" class="easyui-datagrid" title="菜类列表" style="width:500px;height:100%;"
			data-options="singleSelect:true,collapsible:false,rownumbers: true,toolbar:'#tb',onClickCell:cellclick2,onAfterEdit:afterEdit">
		<thead>
			<tr>
				<th data-options="field:'typename',editor:'text'" width="25%">菜类名</th>
				<th data-options="field:'sort',align:'center',formatter:formatSort" width="25%">排序</th>
				<th data-options="field:'id',align:'center',formatter:formatColumn" width="50%">操作</th>

			</tr>
		</thead>
	</table>
	</div>
	
	<div id="tb" style="padding:2px 5px;">
		<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="opendlg()">新增菜类</a>
	</div>
	
	
	
	<div id="dlg" class="easyui-dialog" title="新增菜类" style="width:200px;height:90px;padding:5px;"
			data-options="
				iconCls: 'icon-save',
				modal:true
			">
		 <form id="ff" style="margin:0px">
		<input class="easyui-textbox"  name="typename" id="typename" style="width:100%;height:40px;" data-options="buttonText:'新增'"/>
		</form>
	</div>
	
	
	<div id="dlg1" class="easyui-dialog" title="菜单列表"  style="width:400px;height:400px;">
	<table id="table1" class="easyui-datagrid"  style="width:100%;height:100%;"
			data-options="singleSelect:true,collapsible:false,rownumbers: true,onClickCell:cellclick3,onAfterEdit:afterEdit1">
		<thead>
			<tr>
				<th data-options="field:'foodname'" width="40%">菜名</th>
				<th data-options="field:'price',editor:'text'" width="30%">价格</th>
				<th data-options="field:'id',align:'center',formatter:formatColumn1" width="30%">查看</th>

			</tr>
		</thead>
	</table>
</div>
	
</body>
</html>